package cn.iocoder.yudao.module.agent.controller.app.brokerageuser;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.agent.controller.admin.brokerageuser.vo.AgentBrokerageUserPageReqVO;
import cn.iocoder.yudao.module.agent.controller.admin.brokerageuser.vo.AgentBrokerageUserRespVO;
import cn.iocoder.yudao.module.agent.convert.brokerageuser.AgentBrokerageUserConvert;
import cn.iocoder.yudao.module.agent.dal.dataobject.brokerageuser.AgentBrokerageUserDO;
import cn.iocoder.yudao.module.agent.service.brokerageuser.AgentBrokerageUserService;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "APP - 分销员信息")
@RestController
@RequestMapping("/agent/brokerage-user")
@Validated
public class AppAgentBrokerageUserController {
    @Autowired
    private AgentBrokerageUserService brokerageUserService;

    @Resource
    private MemberUserApi memberUserApi;

    @GetMapping("/page")
    @Operation(summary = "获得代理人分销用户分页")
    public CommonResult<PageResult<AgentBrokerageUserRespVO>> getBrokerageUserPage(@Valid AgentBrokerageUserPageReqVO pageVO) {
        pageVO.setUserId(null);
        pageVO.setCreateTime(null);
        pageVO.setAgentUserId(getLoginUserId());

        PageResult<AgentBrokerageUserDO> pageResult = brokerageUserService.getBrokerageUserPage(pageVO);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), AgentBrokerageUserDO::getUserId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        Set<Long> agentMemberIds = convertSet(pageResult.getList(), AgentBrokerageUserDO::getAgentUserId);
        Map<Long, MemberUserRespDTO> agentMemberMap = memberUserApi.getUserMap(agentMemberIds);

        return success(AgentBrokerageUserConvert.INSTANCE.convertPage(pageResult, memberMap, agentMemberMap));
    }

    @GetMapping("/count")
    @Operation(summary = "获得代理人分销员数量")
    private CommonResult<Long> getBrokerageUserCount(){
        Long agentUserId = getLoginUserId();
        Long count = brokerageUserService.count(agentUserId);

        return success(count);
    }
}
